﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 密码修改
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class PasswordController : AdminBaseController
    {
       /// <summary>
       /// 密码修改
       /// </summary>
       /// <returns></returns>
        [AuthorityActionFilter("m_PrvChgPassword", Order = 1)]
        [LogPageActionFilter("密码更改", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        ///判断用户的用户名和密码是否正确
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns> 
        [HttpPost]
        public string ExitstByPassword(string password) {
            string str = string.Empty;
            BLL.PrvUser userBll = new BLL.PrvUser();
            Model.PrvUser userObj = userBll.GetModelByNameAndPassword(OperateSession.get(GlobalConstant.PARAM_LOGINID),Fetch.estimateString(password, CheckGetEnum.Safety),GlobalConstant.ENTITYNO);
            if (null != userObj)
            {
                str = "Y";
            }
            else
            {
                str = "N";
            }
            return str;
        }

        /// <summary>
        /// 修改密码确定
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditPassword()
        {
            string message = string.Empty;
            string password = Fetch.estimateString(EditorExtension.GetValue<string>("tbPassword"), CheckGetEnum.Safety);
            BLL.PrvUser userBll = new BLL.PrvUser();
            Model.PrvUser userObj = userBll.GetModelByNameAndPassword(OperateSession.get(GlobalConstant.PARAM_LOGINID), password, GlobalConstant.ENTITYNO);
            if (null == userObj)
            {
                message = "原密码错误!";
            }
            else
            {
                string newPassword = EditorExtension.GetValue<string>("tbNewPassword");
                try
                {
                    userBll.EditPassword(OperateSession.get(GlobalConstant.PARAM_LOGINID), newPassword, GlobalConstant.ENTITYNO);
                    message = "密码修改成功!";
                }
                catch (Exception)
                {
                    message = "密码修改失败";
                }

            }
            ViewData["message"] = message;
            return View("Index");
        }
    }
}
